package com.voltmao.controller.module.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.service.application.intent.IntentUserServiceImpl;
import com.voltmao.service.application.web.WebUserServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.MD5Utils;
import com.voltmao.webpro.utils.StringUtils;
import com.voltmao.webpro.utils.aliyun.MsgUtil;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/module/web")
public class WebUserController {
    @Autowired
    WebUserServiceImpl webUserService;
    @Autowired
    IntentUserServiceImpl intentUserService;

    @RequestMapping(value = "/register.json")
    @ResponseBody
    /**
    * 注册
    * */
    public ApiResult register(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                Map<String, Object> resultMap = new HashMap<String, Object>();
                try{
                    webUserService.register(params);
                    apiResult.setStatus(Constants.SUCCESS);

                }catch (Exception e){
                    apiResult.setStatus(Constants.REG_FAIVAL);
                    resultMap.put("error",e.getMessage());
                }
                return resultMap;
            }
        });
    }

    @RequestMapping(value = "/verificationCode.json")
    @ResponseBody
    /**
    * 获取验证码
    * */
    public ApiResult getVerificationCode(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {

                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                Map<String, Object> resultMap = new HashMap<String, Object>();
                //flag为1时，验证手机号是否重复
                String flag = params.get("flag")+"";
                if(!StringUtils.isNotEmpty(params.get("phoneNo"))){
                    apiResult.setStatus(Constants.PHONE_FAIVAL);
                    resultMap.put("error","手机号码未提交");
                    return resultMap;
                }
                String phoneNo = params.get("phoneNo").toString();

                if(phoneNo.length()!=11){
                    apiResult.setStatus(Constants.PHONE_FAIVAL);
                    resultMap.put("error","手机号码格式有误");
                }else{
                    //判断手机号是否重复
                    if(flag.equals("1")){
                        if(!intentUserService.isPhoneOk(phoneNo)){
                            apiResult.setStatus(Constants.PHONE_EXIST);
                            return resultMap;
                        }
                    }

                    Map<String, Object> tmp =  MsgUtil.sendVeriftCodeMsg(phoneNo);
                    String tmp_code = tmp.get("verificationCode")+"";
                    String tmp_status = tmp.get("status")+"";
                    if(tmp_status.equals("200")){
                        resultMap.put("verificationCode",MD5Utils.getPwd(tmp_code));
                    }else{
                        apiResult.setStatus(Constants.SEND_VERIF_CODE_FAIAL);
                    }

                }
                return resultMap;
            }
        });
    }



    /**
     * 渠道忘记密码，验证手机号重新设置
     * @param request
     * @return
     */
    @RequestMapping(value = "/forgetPwd.json")
    @ResponseBody
    public ApiResult forgetPwd(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                Map<String, Object> res = new HashMap<>(2);
                try{
                    webUserService.resetPwdForIntent(params);
                }catch (Exception e){
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    res.put("error",e.getMessage());
                }

                return res;
            }
        });
    }
}
